/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package btnhom;
import java.io.*;
import java.sql.Connection;
import oracle.jdbc.OracleResultSet;
import oracle.jdbc.OraclePreparedStatement;
import conn.JavaConnectDb;
import javax.swing.JOptionPane;

/**
 *
 * @author lhqbao
 */
public class Phuongxa {
    private Integer ma_px;
    private String ten_px;
    
    Connection conn;
    OraclePreparedStatement pst;
    OracleResultSet rs;

    public Integer getMa_px() {
        return ma_px;
    }

    public void setMa_px(Integer ma_px) {
        this.ma_px = ma_px;
    }

    public String getTen_px() {
        return ten_px;
    }

    public void setTen_px(String ten_px) {
        this.ten_px = ten_px;
    }
    public OracleResultSet SelectPhuongXa(){
        conn = JavaConnectDb.ConnectDb();
        try{
            String sql = "select * from phuongxa";
            pst=(OraclePreparedStatement) conn.prepareStatement(sql);
            rs = (OracleResultSet) pst.executeQuery();
            
         }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Khong the ket noi server");
        }
        return rs;
        
    } 
    
    public void insertPhuongxa(String mapx, String tenpx, String maqh)
    {
        conn = JavaConnectDb.ConnectDb();
        try{
            
            String sql = "insert into phuongxa(ma_phuong, ten_phuong, ma_qh) values(?,?,?)";
            pst=(OraclePreparedStatement) conn.prepareStatement(sql);
            pst.setString(1, mapx.trim());
            pst.setString(2, tenpx.trim());
            pst.setString(3, maqh.trim());
            pst.executeQuery();
                    
         }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }finally {
        //try { rs.close(); } catch (Exception e) { /* ignored */ }
       // try { pst.close(); } catch (Exception e) { /* ignored */ }
        try { conn.close(); } catch (Exception e) { /* ignored */ }
        }
    }
    public void updatePhuongxa(String mapx, String tenpx)
    {
        conn = JavaConnectDb.ConnectDb();
        try{
            String sql = "update phuongxa set ten_phuong=? where ma_px=?";
            pst=(OraclePreparedStatement) conn.prepareStatement(sql);
            pst.setString(2, mapx);
            pst.setString(1, tenpx);
            pst.executeQuery();
            JOptionPane.showMessageDialog(null, "Cap nhat thanh cong");        
         }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Cap nhat khong thanh cong");
        } finally {
       // try { rs.close(); } catch (Exception e) { /* ignored */ }
        //try { pst.close(); } catch (Exception e) { /* ignored */ }
        try { conn.close(); } catch (Exception e) { /* ignored */ }
        }
    }
    public void deletePhuongxa(String mapx)
    {
        conn = JavaConnectDb.ConnectDb();
        try{
            String sql = "delete from phuongxa where ma_phuong=?";
            pst=(OraclePreparedStatement) conn.prepareStatement(sql);
            pst.setString(1, mapx);
            pst.executeQuery();
            JOptionPane.showMessageDialog(null, "Xoa thanh cong");        
         }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Xoa khong thanh cong");
        } finally {
        //try { rs.close(); } catch (Exception e) { /* ignored */ }
        //try { pst.close(); } catch (Exception e) { /* ignored */ }
        try { conn.close(); } catch (Exception e) { /* ignored */ }
        }
    }
}
